package com.spring.security.study.security_test.utils;

import com.alibaba.fastjson.JSON;
import com.spring.security.study.security_test.api.SysUserDetails;
import com.spring.security.study.security_test.api.TokenEntry;
import io.jsonwebtoken.Jwts;
import io.jsonwebtoken.SignatureAlgorithm;
import lombok.extern.slf4j.Slf4j;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;


/**
 * @ClassName : TokenUtils
 * @Author : lby
 * @Date: 2021/1/25 14:41
 * @Description : Token工具类
 */
@Slf4j
public class TokenUtils {

    /**
     * Token 生成
     */
    public static String createToken(SysUserDetails sysUser) {
        String token = Jwts.builder()
                //放入用户名和id
                .setId(sysUser.getUserId() + "")
                //主题
                .setSubject(sysUser.getUsername())
                // 角色信息
                .claim("authorities", JSON.toJSONString(sysUser.getAuthorities()))
                //签名算法和密钥
                .signWith(SignatureAlgorithm.HS512, TokenEntry.TOKEN_SECRET_KEY)
                .compact();
        return token;
    }

}
